import pymysql
from conn import configs
from uuid import uuid4
from pymysql.cursors import DictCursor



class MysqlConn:

    def __init__(self):
        self.conn = pymysql.connect(
            host=configs.mysql_sql_host,
            user='root',
            password=configs.mysql_sql_password,
            database='major'
        )

    def insert(self,table_name, datas):
        if not datas:
            return []
        for d in datas:
            d['id']=str(uuid4())
        with self.conn.cursor() as cursor:
            sql = f"INSERT INTO {table_name} ({','.join(datas[0].keys())}) VALUES ({','.join(['%s'] * len(datas[0]))})\n"
            ds = [tuple(d.values()) for d in datas]
            cursor.executemany(sql,ds)
        self.conn.commit()
        return [d['id'] for d in datas]

    def check_entitys_exist(self,entity_names):
        with self.conn.cursor() as cursor:
            sql = f"SELECT name FROM entitys WHERE name IN ({','.join(['%s'] * len(entity_names))})"
            cursor.execute(sql,entity_names)
            return [d[0] for d in cursor.fetchall()]

    def search_news(self):
        with self.conn.cursor(DictCursor) as cursor:
            sql = f"SELECT id,title_zh,created FROM news WHERE DATE(created) >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) ORDER BY created DESC;"
            cursor.execute(sql)
            return cursor.fetchall()
    def search_news_by_urls(self,urls):
        urls = '"' + '","'.join(urls) + '"'
        with self.conn.cursor(DictCursor) as cursor:
            sql = f"SELECT id,title_zh,created FROM news " \
                  f"WHERE url IN ({urls})"
            cursor.execute(sql)
            return cursor.fetchall()

    def search_news_by_id(self,news_id):
        with self.conn.cursor(DictCursor) as cursor:
            sql = f"SELECT id,title_zh,content_zh,created,url,entitys,is_protected FROM news WHERE id = '{news_id}';"
            cursor.execute(sql)
            return cursor.fetchone()

    def search_entitys(self,entity_ids_str):
        with self.conn.cursor(DictCursor) as cursor:
            entity_ids_str = '"' + entity_ids_str.replace(',','","') + '"'
            sql = f"SELECT * FROM entitys WHERE id IN ({entity_ids_str});"
            cursor.execute(sql)
            return cursor.fetchall()


if __name__ == '__main__':
    mysql = MysqlConn()
    #mysql.insert('entitys',[{'name':'李四'}])
    #a = mysql.check_entitys_exist(['张三','王五'])
    #print(a)
    #print(mysql.check_entitys_exist(['张三']))
    print(mysql.search_news())
    a = mysql.search_news_by_id('b7b08671-c596-425f-825b-6df4a3258b3d')
    print(a)
    print(a['entitys'])
    print(mysql.search_entitys(a['entitys']))
    urls = mysql.search_news_by_urls(['item?id=44901528', 'https://www.seangoedecke.com/real-reasoning/', 'https://www.seangoedecke.com/model-on-a-mbp/', 'https://www.quantamagazine.org/the-ai-was-fed-sloppy-code-it-turned-into-something-evil-20250813/', 'https://learn.convo-lang.ai/', 'https://github.com/trvon/yams', 'https://video.golpoai.com/', 'https://www.ycombinator.com/companies/mbodi-ai/jobs/ftTsxcl-founding-research-engineer', 'https://docs.hyprnote.com/owhisper/what-is-this'])
    print(urls)